<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use think\Model;

class ProxyWithdraw extends Model{

    protected $autoWriteTimestamp = true;

    /**
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field=''){
        $field = $field ? $field: "{$this->getTable()}.*,think_proxy.name as proxy_name";
        $list = $this
            ->field($field)
            ->join('think_proxy',"think_proxy.id = {$this->getTable()}.proxy_id")
            ->order($this->getTable().'.create_time desc')
            ->where($where)
            ->paginate($pageSize);
        return $list;
    }

    /**
     * 更新提现订单
     * @param int $order_id
     * @param string $desc
     * @param string $sent
     * @param string $hblist
     * @param null $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function decMoneyByOrderId($order_id=0,$desc='',$sent='',$hblist='',&$out=null){
        $info = $this->where('order_id','eq',$order_id)->find();
        if(!$info){
            $out = '提现不存在';
            return false;
        }
        $this->startTrans();
        $proxy = (new Proxy())->get($info['proxy_id']);
        if($info['status']!=2){
            $proxy->dong_money = $proxy['money'] - $info['money'];
            $info->status = 2;
            $info->update_time = time();
            $info->info = $desc;
            $info->sent_info = $sent;
            $info->hblist = $hblist?json_encode($hblist):'';
            $result = $info->save();
            $result2 = $proxy->save();
        }else{
            $result = $info->sent_info = $sent;
            $result2 = true;
        }
        if($result && $result2){
            $this->commit();
            return true;
        }else{
            $this->rollback();
            $out = '领取失败';
            return false;
        }
    }

    /**
     * 格式时间
     * @param $value
     * @return false|string
     */
    public function getUpdateTimeAttr($value){
        return  $value?date('Y-m-d H:i:s',$value):'--';
    }

    /**
     *
     * @param $name
     * @param $money
     * @param int $proxy_id
     * @param string $out
     * @return $this|bool
     */
    public function addNew($name='',$money=0,$proxy_id=0,&$out=''){
        $valid = new \app\index\validate\ProxyWithdraw();
        $data = [
            'name'=>$name,
            'proxy_id'=>$proxy_id,
            'money'=>$money,
            'status'=>1,
            'order_id'=>build_order_no()
        ];
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        $this->data($data);
        return $this;
    }

    /**
     * 获取数据
     * @param int $id
     * @param string $field
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getListByProxyId($id=0,$field='*',$limit=10){
        $list = $this
            ->field($field)
            ->where('proxy_id','eq',$id)
            ->paginate($limit);
        return $list;
    }

    /**
     * @param $value
     * @return string
     */
    protected function getInfoAttr($value){
        return $value?$value:'--';
    }
}